package com.project.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.project.bean.CustomerBean;
import com.project.bean.SupplierBean;
import com.project.mapper.ICustomerMapper;
import com.project.service.ICustomerService;
import org.apache.ibatis.annotations.DeleteProvider;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@Transactional
public class CustomerServiceImp implements ICustomerService {
    @Autowired
    private ICustomerMapper customerMapper;
    @Override
    public IPage<CustomerBean> findByItem(Integer pageNO, String customerName, String scope) {
        QueryWrapper<CustomerBean> query = new QueryWrapper<CustomerBean>();
        if (customerName!= null && !customerName.equals("")) {
            query.like("c_name",customerName);
        }
        if (scope!= null &&!scope.equals("")) {
            query.like("c_scope",scope);
        }
        return customerMapper.selectPage(new Page<CustomerBean>(pageNO,3),query);
    }

    @Override
    public CustomerBean findById(Integer id) {
        return customerMapper.selectById(id);
    }

    @Override
    public void updateBySupplierId(Integer customerId, String newHeadName, String newHeadPhone, String scope) {
        CustomerBean customerBean = customerMapper.selectById(customerId);
        if (newHeadName!= null && !newHeadName.equals("")) {
            customerBean.setHeadName(newHeadName);
        }
        if (newHeadPhone!= null && !newHeadPhone.equals("")) {
            customerBean.setHeadPhone(newHeadPhone);
        }
        customerMapper.updateById(customerBean);
    }

    @Override
    public void addCustomer(CustomerBean customerBean) {
        QueryWrapper<CustomerBean> qw = new QueryWrapper<>();
        qw.eq("c_name", customerBean.getCustomerName());
        CustomerBean sp = customerMapper.selectOne(qw);
        if (sp == null) {
            customerMapper.insert(customerBean);
        }
    }
}
